import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;


public class AnwesenheitsGUI implements ActionListener
{
	private JButton ja;
	private JButton nein;
	private Student currentStudent;
	private int studentNr = 0;
	Vorlesung v;
	JLabel name;
	
	public AnwesenheitsGUI() throws JAXBException
	{
		v= getVorlesung();
		
		JFrame f = new JFrame();
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setVisible(true);
		
		JPanel anwesenheitsKontrolle = new JPanel();
		f.setContentPane(anwesenheitsKontrolle);
		f.setSize(300,300);
		
		
		name = new JLabel("Enes ...");
		ja = new JButton("Ja");
		nein = new JButton("Nein");
		
		ja.addActionListener(this);
		nein.addActionListener(this);
		
		anwesenheitsKontrolle.add(name);
		anwesenheitsKontrolle.add(ja);
		anwesenheitsKontrolle.add(nein);
		
		
		
		setNextStudent();
	}
	
	
	
	private void setNextStudent()
	{	
		currentStudent = v.getStudentenListe().get(studentNr);
		name.setText(currentStudent.getVorname() + " " + currentStudent.getNachname());
		
		
		
	}



	private Vorlesung getVorlesung() throws JAXBException
	{
		JAXBContext anwesenheitsListe = JAXBContext.newInstance(Vorlesung.class);
		Unmarshaller unmarshaller = anwesenheitsListe.createUnmarshaller();
		Vorlesung vorlesung = (Vorlesung)unmarshaller.unmarshal(new File("anwesenheit.xml"));
		
		return vorlesung;
	}

	@Override
	public void actionPerformed(ActionEvent e)
	{
		Anwesenheit a = new Anwesenheit();
		
		if(e.getSource() == nein)
			a.setAnwesend(false);
		else if(e.getSource() == ja)
			a.setAnwesend(true);
		
		currentStudent.getAnwesenheitsListe().add(a);
		
		setNextStudent();
	}
	
}
